import { CloudSyncService } from './../service/cloudSyncService';
import { OrderService } from './../service/orderService';
import { DeliveryOrderService } from './../service/deliveryOrderService';
import { Response, ServiceConfig, route, Guid, uniqueArray, intersectionArray } from '../common';
import { DeliveryOrder } from '../domain/model';
import { WebException } from './webException';
import * as lodash from 'lodash';
import assert = require("assert");
import * as Koa from "koa";
import * as R from 'ramda';
import { ExpeditionOrderService } from "../service/expeditionOrderService";

export class DeliveryOrderController {
    private deliveryOrderService: DeliveryOrderService;
    private expeditionOrderService: ExpeditionOrderService;
    private orderService: OrderService;
    private syncSvr: CloudSyncService;
    private origin = 'Deliver';
    constructor() {
        this.deliveryOrderService = ServiceConfig.Current.GetService<DeliveryOrderService>("deliveryOrderService");
        this.expeditionOrderService = ServiceConfig.Current.GetService<ExpeditionOrderService>("expeditionOrderService");
        this.orderService = ServiceConfig.Current.GetService<OrderService>("orderService");
        this.syncSvr = ServiceConfig.Current.GetService<CloudSyncService>("cloudSyncService");
    }

    toString() {
        return "DeliveryOrderController";
    }

    // 获取传菜单序列号
    @route({ method: "get", path: "/api/deliveryOrders/serialNO" })
    async getDeliveryOrderSerialNO(ctx: Koa.Context, next) {
        const serialNO = await this.generateDeliveryOrderSerialNO()
        ctx.response.body = Response.ok("获取传菜单序列号", {
            serialNO
        });
        await next();
    }

    //传菜
    @route({ method: "post", path: "/api/deliveryOrders" })
    async deliveryBatchDeliveryOrder(ctx: Koa.Context, next) {
        const storeId = ctx.accept.headers["storeid"];
        const terminal = ctx.request.body.terminal;
        const user = ctx.request.body.user;

        const ids = ctx.request.body.ids;
        const deliverer = ctx.request.body.deliveryer;
        const nextSerialNO = await this.generateDeliveryOrderSerialNO();
        // 检查菜是否已经被传走
        const orders = await this.expeditionOrderService.deliveryBatchExpeditionOrder(ids, deliverer);
        const deliveryOrder = await this.deliveryOrderService.deliveryOrder(
            ids,
            deliverer,
            nextSerialNO,
            terminal.id
        )
        ctx.response.body = Response.ok("传菜成功", {
            orders: [deliveryOrder]
        });

        this.syncDeliveryOrders(deliveryOrder, storeId, terminal, user)

    }

    async generateDeliveryOrderSerialNO() {
        const serialNO = await this.orderService.SerialNoMaker(this.origin);
        return serialNO.replace(this.origin.toString() + "-", "");
    }

    syncDeliveryOrders(deliveryOrder: any, storeId, terminal, user) {
        //删除不要数据
        
        this.syncSvr.DomainDataSyncPackage(
            [
                {
                    id: deliveryOrder.id,
                    body: deliveryOrder,
                    entityName: "deliverys"
                }
            ]
            , {
                storeId,
                terminal,
                user
            }
        );
    }
}